unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, EncryDecryTool,Windows;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    Edit1: TEdit;
    Edit2: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Memo1: TMemo;
    SelectDirectoryDialog1: TSelectDirectoryDialog;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
  private

  public

  end;

var
  Form1: TForm1;
  inputDir:string;
  outputDir:string;
  enc:boolean=true;
implementation

{$R *.lfm}

{ TForm1 }

{$region '搜索目录'}
procedure searchFile(const strPath:string);
var
  found:integer;
  rc:TSearchRec;
  inputfile:String;
  outfile:String;
  FilePath:String;
begin
  found:=findfirst(strPath+'\*.*',faAnyFile,rc);
  while found=0 do
    begin
      if (rc.Attr and faDirectory) <> 0 then
        begin
          if (rc.Name <> '.')and(rc.Name <> '..') then
            begin
              //
              searchFile(strPath+'\'+rc.Name);
            end;
        end
      else
        begin
          if (rc.Name <> '.')and(rc.Name <> '..') then
            begin
              inputfile:=strPath+'\'+rc.Name;
              outfile:=Form1.Edit2.Text+StringReplace(inputfile,Form1.Edit1.Text,'',[]);
              //获取文件目录
              FilePath:=ExtractFilePath(outfile);
              //如果目录不存在，则创建目录
              if not DirectoryExists(FilePath) then
                ForceDirectories(FilePath);
              //过滤Thumbs.db文件
              if rc.Name<>'Thumbs.db' then
                begin
                  if enc then
                    EncryFile(inputfile,outfile,'(.*\.mp3$)|(.*\.fla$)')
                  else
                    DecryFile(inputfile,outfile,'(.*\.mp3$)|(.*\.fla$)');
                  Form1.Memo1.Lines.add(strPath+'\'+rc.Name);
                end;
            end;
        end;
      found:=FindNext(rc);
    end;
end;
{$endregion}

procedure TForm1.Button1Click(Sender: TObject);
begin
  if SelectDirectoryDialog1.Execute then
  begin
    inputDir:=SelectDirectoryDialog1.files.strings[0];
    Edit1.Text:=inputDir;
    //Memo1.Lines.add(SelectDirectoryDialog1.files.strings[0]);
    //FreeAndNil(SelectDirectoryDialog1);
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  if SelectDirectoryDialog1.Execute then
  begin
    outputDir:=SelectDirectoryDialog1.files.strings[0];
    Edit2.Text:=outputDir;
  end;
end;
{$region '解密'}
procedure TForm1.Button3Click(Sender: TObject);
begin
  Memo1.Clear;
  enc:=False;
  if (Edit1.Text='') or (Edit2.Text='') then
    begin
      ShowMessage('输入目录或输出目录不是有效路径');
    end
  else if DirectoryExists(Edit1.Text) and DirectoryExists(Edit2.Text) then
    begin
      searchFile(inputDir);
    end
  else
    begin
      ShowMessage('输入目录或输出目录不是有效路径');
    end;
end;
{$endregion}
{$region '加密'}
procedure TForm1.Button4Click(Sender: TObject);
begin
  Memo1.Clear;
  enc:=True;
  //ShowMessage(Edit1.Text);
  if (Edit1.Text='') or (Edit2.Text='') then
    begin
      ShowMessage('输入目录或输出目录不是有效路径');
    end
  else if DirectoryExists(Edit1.Text) and DirectoryExists(Edit2.Text) then
    begin
      searchFile(inputDir);
    end
  else
    begin
      ShowMessage('输入目录或输出目录不是有效路径');
    end;
end;
{$endregion}
end.

